//
//  MessageCodeViewController.m
//  WBBaseProject
//
//  Created by 吴斌 on 2018/3/19.
//  Copyright © 2018年 吴斌. All rights reserved.
//

#import "MessageCodeViewController.h"
#import "IDVertificationCodeInputView.h"

@interface MessageCodeViewController (){
    dispatch_source_t _timer;
    IDVertificationCodeInputView *vertificationCodeInputView; 
    UIButton *_getCodeBtn;
}

@end

@implementation MessageCodeViewController

#pragma mark - 生命周期 Life Circle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"登录";
    [self setUpUI];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [vertificationCodeInputView becomeFirstResponder];  
}

- (void)setUpUI
{
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    scrollView.contentSize = CGSizeMake(kScreenWidth, kScreenHeight + 1);
    [self.view addSubview:scrollView];
    
    UILabel *titleLb = [[UILabel alloc] initWithFrame:CGRectMake(35, 90, 200, 30)];
    titleLb.text = @"输入短信验证码";
    titleLb.font = [UIFont systemFontOfSize:25];
    [scrollView addSubview:titleLb];
    
    UILabel *phoneLb = [[UILabel alloc] initWithFrame:CGRectMake(35, titleLb.mm_bottom + 20, kScreenWidth - 70, 25)];
    phoneLb.text = @"已发送短信验证码至1881280800";
    phoneLb.textColor = WB_RGBColor(181, 181, 181);
    [scrollView addSubview:phoneLb];
    
    //验证码
    vertificationCodeInputView = [[IDVertificationCodeInputView alloc]initWithFrame:CGRectMake(50,phoneLb.mm_bottom + 30,kScreenWidth - 100,45)];  
    //vertificationCodeInputView.backgroundColor =[UIColor redColor];  
    vertificationCodeInputView.secureTextEntry = NO;  
    vertificationCodeInputView.blockCode = ^(NSString *code) {
        NSLog(@"%@",code);  
        
    };
    [scrollView addSubview:vertificationCodeInputView];  
    
    
    //获取验证码按钮
    UIButton *getCodeBtn = [UIButton ba_buttonWithFrame:CGRectMake(50, vertificationCodeInputView.mm_bottom + 20, 200, 21) title:@"获取验证码" titleColor:WB_RGBColor(181, 181, 181) titleFont:[UIFont systemFontOfSize:12]];
    getCodeBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    _getCodeBtn = getCodeBtn;
    getCodeBtn.tag = 1001;
    [getCodeBtn addTarget:self action:@selector(clickMethod:) forControlEvents:UIControlEventTouchUpInside];
    [scrollView addSubview:getCodeBtn]; 
    
    //确定
    UIButton *sureBtn = [Factory customButtonWithTitle:@"确定"];
    sureBtn.tag = 1000;
    sureBtn.frame = CGRectMake(35, getCodeBtn.mm_bottom + 50, kScreenWidth - 70, 60);
    [sureBtn addTarget:self action:@selector(clickMethod:) forControlEvents:UIControlEventTouchUpInside];
    [scrollView addSubview:sureBtn];
    
    UIBarButtonItem *leftBarItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"返回"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]  style:UIBarButtonItemStylePlain target:self action:@selector(clickMethod:)];
    leftBarItem.tag = 1000;
    self.navigationItem.leftBarButtonItem = leftBarItem;
}

- (void)clickMethod:(UIButton *)sender
{
    if (sender.tag == 1000) {
        [self.navigationController popViewControllerAnimated:YES];
    }else if (sender.tag == 1001) {
        WB_DISPATCH_MAIN_THREAD(^{
            _timer = [Factory startTimeWithBtn:_getCodeBtn];
        });
    }
}




@end
